package com.bit.demo13;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

//自行实现一个简单的线程池，只实现一个固定数目的线程池
class MyFixedThreadPool{
    private BlockingQueue<Runnable> queue = new LinkedBlockingDeque<>();
    public MyFixedThreadPool(int n){
        for(int i=0;i<n;i++){
            Thread t=new Thread(()->{
               try {
                   while(true){
                       Runnable task=queue.take();
                       task.run();
                   }
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
            });
            t.start();
        }
    }
    public void submit(Runnable task) throws InterruptedException {
            queue.put(task);
    }
}

public class MyFixedPool {
    public static void main(String[] args) throws InterruptedException {
        MyFixedThreadPool pool=new MyFixedThreadPool(4);
        for (int i=0;i<100;i++){
            int id=i;
            pool.submit(()->{
                System.out.println(Thread.currentThread().getName()+"正在执行任务"+id);
            });
        }
    }
}
